﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace huaguwang
{
    public class D_T_Menu_Exp : D_T_Menu
    {
        /// <summary>
        /// 查询该用户的菜单分页数据
        /// </summary>
        public List<M_T_Menu> GetMenuList(int begin, int take, int userid, out int count)
        {
            var tab = "dbo.T_Button t1,dbo.T_Power t2,dbo.T_PowerGroupSon t3";
            var strWhere = @"t1.Flag = 1 and t2.Flag = 1 and t3.Flag = 1 and t1.id = t2.ButtonID 
                    and t2.PowerGroupID=t3.PowerGroupID and t3.UserID = @UserID";

            var countsql = ExpandMSSQL.GetPageSQL(tab, strWhere);
            count =MSSQLHelper.SQLConType.C1.Exp_GetSingle(countsql,
                new SqlParameter("@UserID", SqlDbType.Int) { Value = userid }
                ).Exp_IntTryParse();

            var strSql = ExpandMSSQL.GetPageSQL(
                begin,
                take,
                "*",
                tab,
                "t1.ID",
                strWhere,
                "t1.MenuID desc, t1.ButtonOrderBy asc");

            var list = MSSQLHelper.SQLConType.C1.Exp_GetList(
                strSql,
                dr => new M_T_Menu()
                {
                    id = dr["id"].Exp_IntTryParse(),
                    MenuName = dr["MenuName"].Exp_Trim(),
                    MenuURL = dr["MenuURL"].Exp_Trim(),
                    MenuLevel = dr["MenuLevel"].Exp_IntTryParse(),
                    MenuParentID = dr["MenuParentID"].Exp_IntTryParse(),
                    MenuParentList = dr["MenuParentList"].Exp_Trim(),
                    MenuSonList = dr["MenuSonList"].Exp_Trim()
                },
                new SqlParameter("@UserID", SqlDbType.Int) { Value = userid }
                );

            return list;
        }

        /// <summary>
        /// 获取菜单数据
        /// </summary>
        /// <returns></returns>
        public List<M_T_MenuExp> GetMenuList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT     id, MenuName, MenuURL, MenuParentID, MenuLevel, MenuParentList, MenuSonList, Flag, PName ");
            strSql.Append(" FROM         (SELECT     a.id, a.MenuName, a.MenuURL, a.MenuParentID, a.MenuLevel, a.MenuParentList, a.MenuSonList, a.Flag, ISNULL(b.MenuName, '根菜单') AS PName ");
            strSql.Append(" FROM          dbo.T_Menu AS a LEFT OUTER JOIN ");
            strSql.Append(" dbo.T_Menu AS b ON a.MenuParentID = b.id ");
            strSql.Append(" WHERE      (a.Flag = 1 and a.MenuLevel<2) ");
            strSql.Append(" UNION ALL ");
            strSql.Append(" SELECT     0 AS Expr1, '根菜单' AS Expr2, '根菜单' AS Expr3, - 1 AS Expr4, '0' AS Expr5, '0' AS Expr6, '0' AS Expr7, 1, '无' AS Expr8 ");
            strSql.Append(" ) AS a_1 ");
            strSql.Append(" ORDER BY ID ");

            var list = MSSQLHelper.SQLConType.C1.Exp_GetList(strSql.ToString(),
               DR => new M_T_MenuExp()
               {
                   Flag = DR["Flag"].Exp_BoolTryParse(),
                   id = DR["id"].Exp_IntTryParse(),
                   MenuLevel = DR["MenuLevel"].Exp_IntTryParse(),
                   MenuName = DR["MenuName"].Exp_Trim(),
                   MenuParentID = DR["MenuParentID"].Exp_IntTryParse(),
                   MenuParentList = DR["MenuParentList"].Exp_Trim(),
                   MenuSonList = DR["MenuSonList"].Exp_Trim(),
                   MenuURL = DR["MenuURL"].Exp_Trim(),
                   PID = DR["MenuParentID"].Exp_IntTryParse(),
                   PName = DR["PName"].Exp_Trim(),
               }
               );
            return list;
        }

        /// <summary>
        /// 通过权限组ID查询菜单列表资料
        /// </summary>
        public List<M_T_Menu> GetMenuList(int begin, int take, int PowerGroupID, bool flag)
        {
            var sb = new StringBuilder();
            sb.Append(" select * from dbo.T_Menu where Flag = 1 ");
            sb.Append(" where Flag = 1 and id in ( ");
            sb.Append("  select distinct(MenuID) from dbo.T_Button ");
            sb.Append(" where Flag = 1 and id in( ");
            sb.Append(" select ID From dbo.T_Button ");
            sb.Append(" where Flag = 1 and id in( ");
            sb.Append(" select ButtonID from dbo.T_Power ");
            sb.Append(" where Flag =1 and PowerGroupID = @pgid )))");

            var list = MSSQLHelper.SQLConType.C1.Exp_GetList(
                sb.ToString(),
                dr => new M_T_Menu()
                {
                    id = dr["id"].Exp_IntTryParse(),
                    MenuName = dr["MenuName"].Exp_Trim(),
                    MenuURL = dr["MenuURL"].Exp_Trim(),
                    MenuLevel = dr["MenuLevel"].Exp_IntTryParse(),
                    MenuParentID = dr["MenuParentID"].Exp_IntTryParse(),
                    MenuParentList = dr["MenuParentList"].Exp_Trim(),
                    MenuSonList = dr["MenuSonList"].Exp_Trim()
                },
                new SqlParameter("@pgid", SqlDbType.Int) { Value = PowerGroupID }
                );

            return list;
        }

        /// <summary>
        /// 判断菜单url是否重复
        /// </summary>
        public bool IsMenuURL(string strURL, int id)
        {
            return MSSQLHelper.SQLConType.C1.Exp_ExistName(" T_Menu ",id, " MenuURL ", strURL, SqlDbType.NVarChar, "");
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        public List<M_T_MenuExp> GetMenuListPage(int Begin, int Pager, string PName, string Name, out int Count)
        {
            //            SELECT     TOP (100) PERCENT a.id, a.MenuName, a.MenuURL, a.MenuParentID, a.MenuLevel, a.MenuParentList, a.MenuSonList, a.Flag, b.MenuName AS PName
            //FROM         dbo.T_Menu AS a INNER JOIN
            //                      dbo.T_Menu AS b ON a.MenuParentID = b.id
            //WHERE     (a.Flag = 1)
            ////ORDER BY a.MenuParentList
            var tab = "dbo.T_Menu AS a left JOIN  dbo.T_Menu AS b ON a.MenuParentID = b.id";
            var cod = " a.flag=1 and a.MenuName like '%'+@Name+'%' and isnull(b.menuName,'根目录') like '%'+@PName+'%' ";

            var countsql = ExpandMSSQL.GetPageSQL(tab, cod);
            Count = MSSQLHelper.SQLConType.C1.Exp_GetSingle(countsql,
                new SqlParameter("@Name", SqlDbType.NVarChar, 50) { Value = Name },
                new SqlParameter("@PName", SqlDbType.NVarChar, 50) { Value = PName }
                ).Exp_IntTryParse();


            var strSql = ExpandMSSQL.GetPageSQL(
                        Begin,
                        Pager,
                        "a.id, a.MenuName, a.MenuURL, a.MenuParentID, a.MenuLevel, a.MenuParentList, a.MenuSonList, a.Flag,isnull( b.MenuName,'根目录') AS PName",
                        "dbo.T_Menu AS a left JOIN  dbo.T_Menu AS b ON a.MenuParentID = b.id",
                        " a.id ",
                        " a.flag=1 and a.MenuName like '%'+@Name+'%' and isnull(b.menuName,'根目录') like '%'+@PName+'%' ",
                        " a.MenuParentList ");

            var list = huaguwang.MSSQLHelper.SQLConType.C1.Exp_GetList(strSql,
                DR => new M_T_MenuExp()
                {
                    Flag = DR["Flag"].Exp_BoolTryParse(),
                    id = DR["id"].Exp_IntTryParse(),
                    MenuLevel = DR["MenuLevel"].Exp_IntTryParse(),
                    MenuName = DR["MenuName"].Exp_Trim(),
                    MenuParentID = DR["MenuParentID"].Exp_IntTryParse(),
                    MenuParentList = DR["MenuParentList"].Exp_Trim(),
                    MenuSonList = DR["MenuSonList"].Exp_Trim(),
                    MenuURL = DR["MenuURL"].Exp_Trim(),
                    PID = DR["MenuParentID"].Exp_IntTryParse(),
                    PName = DR["PName"].Exp_Trim(),
                },
                new SqlParameter("@Name", SqlDbType.NVarChar, 50) { Value = Name },
                new SqlParameter("@PName", SqlDbType.NVarChar, 50) { Value = PName }
                );


            return list;

        }

        public int GetSonCount(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  count(id) as id from T_Menu ");
            strSql.Append(" where MenuParentID=@id and flag=1 ");
            return huaguwang.MSSQLHelper.SQLConType.C1.Exp_GetSingle(
                strSql.ToString(),
                new SqlParameter("@id", SqlDbType.Int) { Value = id }
                ).Exp_IntTryParse();
        }
        public int GetButtonCount(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  count(id) as id from T_Button ");
            strSql.Append(" where MenuID=@id and flag=1 ");
            return huaguwang.MSSQLHelper.SQLConType.C1.Exp_GetSingle(
                strSql.ToString(),
                new SqlParameter("@id", SqlDbType.Int) { Value = id }
                ).Exp_IntTryParse();

        }

        public List<M_T_Menu> GetListAllP()
        {

            string strSql = " SELECT     id, MenuName, MenuURL, MenuParentID, MenuLevel, MenuParentList, MenuSonList, Flag " +
                            " FROM         dbo.T_Menu  where flag=1 and menuparentid=0";
            var list = MSSQLHelper.SQLConType.C1.Exp_GetList(strSql,
               DR => new M_T_Menu()
               {
                   Flag = DR["Flag"].Exp_BoolTryParse(),
                   id = DR["id"].Exp_IntTryParse(),
                   MenuLevel = DR["MenuLevel"].Exp_IntTryParse(),
                   MenuName = DR["MenuName"].Exp_Trim(),
                   MenuParentID = DR["MenuParentID"].Exp_IntTryParse(),
                   MenuParentList = DR["MenuParentList"].Exp_Trim(),
                   MenuSonList = DR["MenuSonList"].Exp_Trim(),
                   MenuURL = DR["MenuURL"].Exp_Trim(),
               }
               );
            return list;

        }
        public List<M_T_Menu> GetListSon(int id)
        {

            string strSql = " SELECT     id, MenuName, MenuURL, MenuParentID, MenuLevel, MenuParentList, MenuSonList, Flag " +
                            " FROM         dbo.T_Menu  where flag=1 and menuparentid=@id ";
            var list = MSSQLHelper.SQLConType.C1.Exp_GetList(strSql,
               DR => new M_T_Menu()
               {
                   Flag = DR["Flag"].Exp_BoolTryParse(),
                   id = DR["id"].Exp_IntTryParse(),
                   MenuLevel = DR["MenuLevel"].Exp_IntTryParse(),
                   MenuName = DR["MenuName"].Exp_Trim(),
                   MenuParentID = DR["MenuParentID"].Exp_IntTryParse(),
                   MenuParentList = DR["MenuParentList"].Exp_Trim(),
                   MenuSonList = DR["MenuSonList"].Exp_Trim(),
                   MenuURL = DR["MenuURL"].Exp_Trim(),
               },
               new SqlParameter("@id", SqlDbType.Int) { Value = id }
               );
            return list;

        }

    }
}
